package com.bigdata.schaduler.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.apache.dolphinscheduler.common.enums.Flag;
import org.apache.dolphinscheduler.common.enums.Priority;
import org.apache.dolphinscheduler.common.enums.TaskExecuteType;
import org.apache.dolphinscheduler.common.enums.TimeoutFlag;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition;
import org.apache.dolphinscheduler.plugin.task.api.model.Property;
import org.apache.dolphinscheduler.plugin.task.api.parameters.DependentParameters;

import java.util.ArrayList;
import java.util.List;

@Data
public class TaskDefineParam {
  /**
   * 初始化，需要手动调用方法生成
   */
  private Long code;
  /**
   * 初始化，默认为1
   */
  private Integer version;
  private String name;
  private String description;
  /**
   * 任务类型标识，比如SHELL、HTTP
   */
  private String taskType;
  /**
   * 任务参数、比如localParams、dependence等等
   * AbstractTask JSON
   */
  private String taskParams;
  /**
   * NO则设置为forbidden任务节点，提交后置节点回跳过该节点
   * 但是判断是否提交该节点的后置节点时，会判断该forbidden节点的前置任务preTasks有没有完成
   */
  private String flag = Flag.YES.getDescp();
  /**
   * 项目编码
   */
  private String projectCode;

  /**
   * 任务优先级
   */
  private String taskPriority = Priority.MEDIUM.getDescp();
  /**
   * 任务执行的工作节点，master进行任务调度(cancel、kill、run等等)的工作节点
   */
  private String workerGroup;
  /**
   * 任务分组暂时不用
   */
  private int taskGroupId = 0;
  private int taskGroupPriority = 0;

  /**
   * 失败重试次数和间隔(单位分钟)，重试事件、重建添加到readToSubmitQueue等待向worker节点提交
   */
  private String failRetryTimes;
  private String failRetryInterval;
  /**
   * 任务提交(submit指令)到worker节点，会提交到超时检测队列和任务状态检测队列
   * 超时告警，触发超时告警事件，生成流程timeout、和流程任务timeout两条ALTER
   */
  private String timeoutFlag = TimeoutFlag.CLOSE.getDesc();
  private String timeoutNotifyStrategy;
  private Integer timeout = 0;
  private String delayTime = "0";

  /**
   * 批处理任务BATCH、流任务STREAM
   */
  private String taskExecuteType = TaskExecuteType.BATCH.getDesc();
  /**
   * 环境、配额暂时未用到
   */
  private Integer environmentCode = -1;
  private Integer cpuQuota = -1;
  private Long memoryMax = -1L;
  private String isCache = "NO";

  public void setTaskDefineParam(TaskDefinition taskDefineParam) {
    BeanUtil.copyProperties(taskDefineParam, this);
  }

  public void setParams(DependentParameters dependenceParams, List<Property> propertyList) {
    JSONObject jsonObject;
    if (this.taskParams == null) {
      jsonObject = new JSONObject();
    } else {
      jsonObject = JSONUtil.parseObj(this.taskParams);
    }

    if (dependenceParams != null) {
      jsonObject.putOnce("dependence", dependenceParams);
    }

    if (CollUtil.isNotEmpty(propertyList)) {
      jsonObject.putOnce("localParams", propertyList);
    }

    this.setTaskParams(JSONUtil.toJsonStr(jsonObject));
  }

  public List<TaskDefineParam> setTaskDefineParamList(List<TaskDefinition> taskDefinitionList) {
    List<TaskDefineParam> taskDefineParamList = new ArrayList<>();
    if (CollUtil.isNotEmpty(taskDefinitionList)) {
      taskDefinitionList.forEach(taskDefinition -> {
        TaskDefineParam taskDefineParam = new TaskDefineParam();
        taskDefineParam.setTaskDefineParam(taskDefinition);
        taskDefineParamList.add(taskDefineParam);
      });
    }
    return taskDefineParamList;
  }
}
